#ifndef _LUAANALYZER_H_INCLUDE_
#define _LUAANALYZER_H_INCLUDE_

#include <iostream>
#include <vector>
#include <string>
#include <regex>
#include <map>
#include <fstream>
#include <memory>
#include <sstream>
#include "singleton.h"

// 定义参数信息
struct ParamInfo {
    std::string name;    // 函数名
    std::string comment; // 函数说明
};

// 定义系统对象
struct ObjectInfo {
    std::vector<std::string> getAllParamName(int type);

    inline std::string getParamInfo(std::string paramName)
    {
        for(int i=0;i<functions.size();i++)
        {
            int pos = functions[i].name.find(paramName);
            if(pos >= 0)
                return functions[i].comment;
        }

        for(int i=0;i<params.size();i++)
        {
            int pos = params[i].name.find(paramName);
            if(pos >= 0)
                return params[i].comment;
        }

        return "";
    }

    inline bool isInCreateFuncitons(std::string varName)
    {
        if(varName == "" || createfunction.empty())
            return false;

        for(int i=0;i<createfunction.size();i++)
        {
            int pos = varName.find(createfunction[i]);
            if(pos >= 0)
                return true;
        }

        return false;
    }

    std::vector<ParamInfo> functions;                // 函数列表
    std::vector<ParamInfo> params;                   // 参数列表
    std::string objectName;                          // 对象名称
    std::vector<std::string> createfunction;         // 创建函数
    std::string inheritedclass;                      // 继承类
};

class LuaAnalyzer : public Singleton<LuaAnalyzer>
{
public:
    // Nested structs for analysis results
    struct Variable {
        std::string name;
        std::string type;
        std::string scope;
        int lineNumber;
    };

    struct Function {
        std::string name;
        std::vector<std::string> parameters;
        int lineNumber;
    };

    struct Object {
        std::string name;
        std::string type;
        std::vector<std::string> methods;
    };

    // Constructor
    LuaAnalyzer();
    ~LuaAnalyzer();

    // Load Lua script from file
    bool loadScript(const std::string& filePath);
    // Load Lua script from string
    void loadScriptFromString(const std::string& script);
    bool loadGrammarNotes(const std::string& filePath);
    // Perform analysis
    void analyze();
    void clearResults();
    ObjectInfo* getObjectInfo(std::string objName);
    ObjectInfo* getVariableInfo(std::string varName);
    ObjectInfo* getInheritedObjectInfo(std::string varName);
    std::string getParamInfo(std::string paramName);

    // Get analysis results
    std::map<std::string,Variable> getVariables() { return variables; }
    std::map<std::string,Function> getFunctions() { return functions; }
    std::map<std::string,Object> getObjects() { return objects; }

    // Print analysis results
    std::string printResults();

private:
    std::vector<std::string> scriptContent;
    std::map<std::string,Variable> variables;
    std::map<std::string,Function> functions;
    std::map<std::string,Object> objects;

    void analyzeVariables(const std::string& line, int lineNumber);
    void analyzeFunctions(const std::string& line, int lineNumber);
    void analyzeObjects(const std::string& line);

    std::map<std::string,ObjectInfo> mObjectList;
};
#endif // LUAANALYZER_H
